package com.mhys.hms.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhys.hms.entity.FeedBackObject;
import com.mhys.hms.entity.Reward;
import com.mhys.hms.service.user.RewardManagerService;

@Controller
public class RewardManager {
	
	@Autowired
	private RewardManagerService rewardManagerService;
	
	/**
	 * 全查
	 * @return
	 */
	@RequestMapping(value="/queryReward",method= {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public FeedBackObject queryRewardInfo() {
		//分页查询
		List<Reward> list = rewardManagerService.queryAllReward();
		//封装数据
		Map<String, Object> map = new HashMap<String, Object>();
		FeedBackObject feedbackObject = new FeedBackObject();
		map.put("total", rewardManagerService.queryAllReward());
		map.put("data", list);
		feedbackObject.Obj = map;
		return feedbackObject;
	}
	
	/**
	 *  分页模糊查询
	 * @return
	 */
	@RequestMapping(value="/queryRewardPage",method= {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public FeedBackObject queryRewardPage(int pageSize,int pageNumber,String deptNo,String deptName) {
		FeedBackObject feedbackObject = new FeedBackObject();
		//获取数据
		List<Reward> list = rewardManagerService.queryRewardPage(pageSize, pageNumber, deptNo, deptName);
		//获取总条数
		int rows = rewardManagerService.queryTotalCount(deptNo, deptName);
		//封装数据
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
		map.put("total", rows);
		feedbackObject.Obj = map;
		return feedbackObject;
	}
	
	/**
	 * 删除指定id奖惩
	 * @param rId
	 * @return
	 */
	@RequestMapping(value="/delRewardInfo", method= {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject delRewardInfo(String rId) {
		FeedBackObject fbo = new FeedBackObject();
		Integer row = rewardManagerService.delRewardInfo(rId);
		if (row>0) {
			fbo.success = "OK";
		}else if (row==1) {
			fbo.msg ="该奖励存在员工，不能删除!";
		}else {
			fbo.msg= "网络异常，请稍后重试!";
		}
		return fbo;
	}
	
	/**
	 * 添加
	 * @param reward
	 * @return
	 */
	@RequestMapping(value="/addRewardInfo", method= {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject addRewardInfo(String id, String rContent, String rType, 
			String rMoney, String rTime) {
		FeedBackObject fboBackObject = new FeedBackObject();
		Reward reward = new Reward();
		reward.setrId(id);
		reward.setrContent(rContent);
		reward.setrType(rType);
		reward.setrMoney(Double.parseDouble(rMoney));
		reward.setrTime(initDate(rTime));
		Integer row = rewardManagerService.addRewardInfo(reward);
		if (row>0) {
			fboBackObject.success="OK";
		}else if (row==1) {
			fboBackObject.msg="添加失敗";
		}else {
			fboBackObject.msg="網絡異常，稍後重試";
		}
		return fboBackObject;
	}
	
	/**更新
	 * 
	 * @param id
	 * @param rContent
	 * @param rType
	 * @param rMoney
	 * @param rTime
	 * @return
	 */
	@RequestMapping(value="/updateRewardInfo", method= {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject updateRewardInfo(String id, String rContent, String rType, 
			String rMoney, String rTime) {
		FeedBackObject fbo = new FeedBackObject();
		Reward reward = new Reward();
		reward.setrId(id);
		reward.setrContent(rContent);
		reward.setrType(rType);
		reward.setrMoney(Double.parseDouble(rMoney));
		reward.setrTime(initDate(rTime));
		Integer row = rewardManagerService.updateReward(reward);
		if (row>0) {
			fbo.success="OK";
		} else {
			fbo.success="fail";
		}
		return fbo;
	}
	
	/**
	 * 單查
	 * @param rId
	 * @return
	 */
	@RequestMapping(value="/queryRewardById",method= {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public FeedBackObject queryRewardById(String rId) {
		FeedBackObject fbo=new FeedBackObject();
		//分页查询数据
		List<Reward> list = rewardManagerService.queryRewardById(rId);
		//封装数据
		Map<String,Object> map = new HashMap<String,Object>();
		//返回总页数
		map.put("total", 1);
		//返回总数据
		map.put("data", list);
		fbo.Obj = map;
		return fbo;
	}
	
	//日期格式化
		public Date initDate(String str) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			try {
				date = sdf.parse(str);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return date;
		}
}
